#include "streammagic.h"
#include "playdialog.h"
#include "recorddialog.h"
#include "StreamPipe.h"

#include <string>
#include <cstring>
using namespace std;

#include <qmessagebox.h>
#include <QProcess>

#define TABLE_SIZE 50

time_t StreamMagic::RefTime = 0;
QProcess *PlaybackProcess;

StreamMagic::StreamMagic(QWidget *parent)
    : QMainWindow(parent)
{
	ui.setupUi(this);

	SessionList.clear();

    QStringList headers;
    ui.treeWidget->setColumnCount(2);
    headers << tr("Field") << tr("Value");
    ui.treeWidget->setHeaderLabels(headers);

	RefTime = time(NULL);
	PlaybackProcess = new QProcess(this);
	ui.Screen->show();
}

StreamMagic::~StreamMagic()
{

}

void StreamMagic::on_actionExit_triggered()
{
	PlaybackProcess->close();
	delete PlaybackProcess;
	((QMainWindow*)this)->close();
}

void StreamMagic::on_actionPlay_triggered()
{
	PlayDialog Dialog(this);
	if (Dialog.exec())
	{
		SPI_Option_t Options;
		bool Ret = 0;
		int SPIRet = OK;

		strncpy(Options.SessionID, Dialog.Session().toStdString().c_str(), SPI_MAX_NAME_LENGTH);
		strncpy(Options.Address, Dialog.Address().toStdString().c_str(), IP_ADDR_LENGTH);
		strncpy(Options.OwnAddress, "127.0.0.1", IP_ADDR_LENGTH);
		Options.Port = Dialog.Port().toInt(&Ret);
		if (!Ret)
		{
			Options.Port = DefaultPort;
		}
		strncpy(Options.FileName[0], Dialog.File().toStdString().c_str(), SPI_MAX_NAME_LENGTH);
		Options.ReferenceTime.tv_sec = RefTime;
		Options.MaxCBSize = DefaultCBSize;
		Options.SessionType = Playback;

		SPIRet = SPI_Create(NULL, Options);
		if (SPIRet != OK)
		{
			QMessageBox msgBox;
			msgBox.setText(QString("SPI error number %1 has occurred.").arg(SPIRet));
			msgBox.setIcon(QMessageBox::Critical);
			msgBox.exec();
		}

		Refresh();


		try
		{
			QString St = "mplayer -wid ";
			St += QString::number((int) ui.Screen->winId());
			St += " ";
			St += Dialog.File();
			QMessageBox msgBox;
			msgBox.setText(St);
			msgBox.setIcon(QMessageBox::Information);
			msgBox.exec();
			PlaybackProcess->close();
			PlaybackProcess->start(St);
			ui.Screen->show();
		}
		catch(...)
		{
			QMessageBox msgBox;
			msgBox.setText(QString("Video display error."));
			msgBox.setIcon(QMessageBox::Critical);
			msgBox.exec();
		}

	}
}

void StreamMagic::on_actionRecord_triggered()
{
	RecordDialog Dialog(this);
	if (Dialog.exec())
	{
		SPI_Option_t Options;
		bool Ret = 0;

		strncpy(Options.SessionID, Dialog.Session().toStdString().c_str(), SPI_MAX_NAME_LENGTH);
		strncpy(Options.Address, Dialog.Address().toStdString().c_str(), IP_ADDR_LENGTH);
		strncpy(Options.OwnAddress, "127.0.0.1", IP_ADDR_LENGTH);
		Options.Port = Dialog.Port().toInt(&Ret);
		if (!Ret)
		{
			Options.Port = DefaultPort;
		}
		strncpy(Options.FileName[0], Dialog.File().toStdString().c_str(), SPI_MAX_NAME_LENGTH);
		Options.ReferenceTime.tv_sec = RefTime;
		Options.MaxCBSize = DefaultCBSize;
		Options.SessionType = Record;

		SPI_Create(NULL, Options);
	}
}

void StreamMagic::on_refreshButton_clicked()
{
	Refresh();
}

void StreamMagic::Refresh()
{
	unsigned int TableSize = TABLE_SIZE;
	SPI_StreamPipe_t * SPITable[TABLE_SIZE];
	SPI_List(SPITable, &TableSize);

//	for (int i=0; i<SessionList.count(); i++)
//	{
//		ui.treeWidget->removeItemWidget(SessionList[i], 0);
//	}

	ui.treeWidget->clear();
	SessionList.clear();

	for (unsigned int i = 0; i < TableSize; ++i)
	{
		QStringList List;
		QTreeWidgetItem * TopLevelItem;
		QTreeWidgetItem * SecondLevelItem;

		// Sessions details
		List.append(QString(SPITable[i]->Options.SessionID));
		TopLevelItem = new QTreeWidgetItem(ui.treeWidget, List);
		List.clear();
		SessionList.append(TopLevelItem);

		List.append(QString("Session type"));
		if (SPITable[i]->Options.SessionType == Playback)
		{
			List.append(QString("Playback"));
		}
		else
		{
			List.append(QString("Record"));
		}
		new QTreeWidgetItem(TopLevelItem, List);
		List.clear();

		List.append(QString("Address"));
		List.append(QString(SPITable[i]->Options.Address));
		new QTreeWidgetItem(TopLevelItem, List);
		List.clear();

		List.append(QString("Port"));
		List.append(QString("%1").arg(SPITable[i]->Options.Port));
		new QTreeWidgetItem(TopLevelItem, List);
		List.clear();

		List.append(QString("File(s)"));
		List.append(QString(SPITable[i]->Options.FileName[0]));
		new QTreeWidgetItem(TopLevelItem, List);
		List.clear();

		// StreamReader details
		List.append(QString("StreamReader"));
		SecondLevelItem = new QTreeWidgetItem(TopLevelItem, List);
		List.clear();

		List.append(QString("Total Bytes Read"));
		List.append(QString("%1").arg(SPITable[i]->SR->TotalByteRead));
		new QTreeWidgetItem(SecondLevelItem, List);
		List.clear();

		// StreamAnalyzer details
		List.append(QString("StreamAnalyzer"));
		SecondLevelItem = new QTreeWidgetItem(TopLevelItem, List);
		List.clear();

		List.append(QString("Duration"));
		List.append(QString("%1").arg(SPITable[i]->SA->Duration));
		new QTreeWidgetItem(SecondLevelItem, List);
		List.clear();

		List.append(QString("Total Packet Number"));
		List.append(QString("%1").arg(SPITable[i]->SA->TotalPacketNumber));
		new QTreeWidgetItem(SecondLevelItem, List);
		List.clear();

		List.append(QString("Bitrate"));
		List.append(QString("%1").arg(SPITable[i]->SA->AvgBitrate));
		new QTreeWidgetItem(SecondLevelItem, List);
		List.clear();

		List.append(QString("Total PAT Number"));
		List.append(QString("%1").arg(SPITable[i]->SA->TotalPATNumber));
		new QTreeWidgetItem(SecondLevelItem, List);
		List.clear();

		List.append(QString("Total PMT Number"));
		List.append(QString("%1").arg(SPITable[i]->SA->TotalPMTNumber[0]));
		new QTreeWidgetItem(SecondLevelItem, List);
		List.clear();

		List.append(QString("Total Packet Errors"));
		List.append(QString("%1").arg(SPITable[i]->SA->TotalPacketError));
		new QTreeWidgetItem(SecondLevelItem, List);
		List.clear();

		// StreamPlayer details
		List.append(QString("StreamPlayer"));
		SecondLevelItem = new QTreeWidgetItem(TopLevelItem, List);
		List.clear();

		List.append(QString("Total Bytes Played"));
		List.append(QString("%1").arg(SPITable[i]->SP->TotalBytePlayed));
		new QTreeWidgetItem(SecondLevelItem, List);
		List.clear();

	}
}

